﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FazaDemo.BL.InterfaceDomain;
using FazaDemo.ViewModels;

namespace FazaDemo.PL.Wpf
{
    class NavElementBuilder
    {

        private const int GridTypeIndex = 2;

        private readonly VisualElemBuilder _elemBuilder;

        private readonly WorkArea _workArea;

        private SuppleModalBox _suppleBox;

        private IList<TextBox> _textBoxValues;

        private Grid _parentGrid;

        private string _gridType;

        private IList<string> _disableNameSymbs = new List<string>
            {" ", "/", "-"};

        public NavElementBuilder(Grid parentGrid, WorkArea workArea)
        {
            _parentGrid = parentGrid;
            _elemBuilder = new VisualElemBuilder(_parentGrid);
            _workArea = workArea;
            _textBoxValues = new List<TextBox>();
        }

        public void Build(NavElementViewModel elemModel, int rowIndex)
        {
            _elemBuilder.RowIndex = rowIndex;
            _elemBuilder.CreateWithContent<Label>(elemModel.ElemText, 0);
            _elemBuilder.Create<ComboBox>("", 1);
            _elemBuilder.Create<TextBox>("tb" + GetElemPart(elemModel.ElemText), 2);
            _textBoxValues.Add((TextBox) _parentGrid.Children[_parentGrid.Children.Count - 1]);
            if (elemModel.HaveSuppInfo)
            {
                _elemBuilder.CreateWithClickContent<Button>("СПР", "bs" + GetElemPart(elemModel.ElemText), 
                    On_ButtonNavSuppleClick, 3);
            }
            foreach (var item in Enum.GetValues(typeof(Order)))
            {
                _elemBuilder.CreateWithContent<RadioButton>(item.ToString(), 4);
            }
            _elemBuilder.CreateChecked<CheckBox>(elemModel.IsShowView, 5);
        }

        private void On_ButtonNavSuppleClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button != null)
            {
                _suppleBox = new SuppleModalBox { Owner = _workArea };
                var provider = new DgridFactoryProvider();
                _gridType = button.Name.Substring(GridTypeIndex);
                DataGridFactory factory = provider.Get(_gridType);
                _suppleBox.DgridSupple = factory.Get();
                _suppleBox.DgridSupple.Background = Brushes.Black;
                _suppleBox.DgridSupple.SelectedCellsChanged += On_SelectedChanged;
                _suppleBox.GridMain.Children.Add(_suppleBox.DgridSupple);
                Grid.SetColumn(_suppleBox.DgridSupple, 0);
                Grid.SetColumn(_suppleBox.DgridSupple, 1);
                _suppleBox.ShowDialog();   
            }
        }

        private string GetElemPart(string elemText)
        {
            List<char> resultChars = elemText.TakeWhile(c => !_disableNameSymbs.Contains(c.ToString())).ToList();
            var result = string.Empty;
            foreach (var c in resultChars)
            {
                result += c.ToString();
            }
            return result;
        }

        private void On_SelectedChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            var dataGrid = sender as DataGrid;
            if (dataGrid != null)
            {
                IList<DataGridCellInfo> selCells = dataGrid.SelectedCells;
                var model = selCells[0].Item;
                var factory = new EntityUnloaderFactory();
                IEntityUnloader unloader = factory.Create(model);
                string result = unloader.GetValue();
                _suppleBox.Close();
                TextBox targetBox = _textBoxValues.Where(t => t.Name.Contains(_gridType)).ToList()[0];
                targetBox.Text = result;
            }
        }

    }
}
